import config from 'config';
import genericPool from 'generic-pool';
import redis, { RedisClient as Client } from 'redis';
import { RedisClient } from './index';

const factory = {
  create: (): Client => {
    return redis.createClient(config.get('redis.port'), config.get('redis.host'), {
      db: config.get('redis.db') || 0
    });
  },
  destroy: (client: RedisClient) => {
    client.quit();
  },
};

const opts = {
  max: 10, // maximum size of the pool
  min: 3, // minimum size of the pool
};
// @ts-ignore
const pool = genericPool.createPool(factory, opts);

export { pool };
